<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户">
                <table-select
                  :tableColumns="[{prop:'code',label:'客户编码',width:'100'},{prop:'name',label:'客户名称',width: '280'}]"
                  :value.sync="query.customerIds"
                  placeholder="请选择客户"
                  url="/api/dropDown/basCustomerList"
                ></table-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="商品">
                <table-select
                  :tableColumns="[{prop:'goodsCode',label:'商品编码',width:'100'},{prop:'goodsName',label:'商品名称',width: '280'}]"
                  labelKey="goodsName"
                  valueKey="goodsId"
                  placeholder="请选择商品"
                  :value.sync="query.goodsIds"
                  url="/api/dropDown/basGoodsListForAdd"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <crudOperation :permission="permission">
        <el-button
          type="primary"
          slot="right"
          class="filter-item"
          size="mini"
          @click="adjust()"
          v-permission="permission.adjust"
          :disabled="crud.selections.length > 1"
        >
          调整
        </el-button>
      </crudOperation>
      <!--表单组件-->
<!--      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">-->
<!--        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">-->
<!--          <el-form-item label="id">-->
<!--            <el-input v-model="form.id" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="客户id" prop="customerId">-->
<!--            <el-input v-model="form.customerId" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="商品id" prop="goodsId">-->
<!--            <el-input v-model="form.goodsId" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="数量">-->
<!--            <el-input v-model="form.goodsCount" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="箱数">-->
<!--            <el-input v-model="form.boxCount" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="添加时间">-->
<!--            <el-input v-model="form.createTime" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="修改时间">-->
<!--            <el-input v-model="form.updateTime" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="公司id">-->
<!--            <el-input v-model="form.companyId" style="width: 370px;" />-->
<!--          </el-form-item>-->
<!--        </el-form>-->
<!--        <div slot="footer" class="dialog-footer">-->
<!--          <el-button type="text" @click="crud.cancelCU">取消</el-button>-->
<!--          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>-->
<!--        </div>-->
<!--      </el-dialog>-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <div class="flex flex-row">
            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="客户" prop="customerId">
                <zSelect  :value.sync="form.customerId"  url="/api/dropDown/basCustomerList"
                          :params="{type:1}" style="width:  100%;"/>
              </el-form-item>
              <el-form-item label="商品" prop="goodsId">
                <zSelect :value.sync="form.goodsId" :showName="form.goodsName" valueKey="goodsId" labelKey="goodsName" url="/api/dropDown/basGoodsListForAdd"
                         v-model="form.goodsId"
                         :params="{packageType:2}" style="width:  100%;"/>
              </el-form-item>
            </el-col>

            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
              <el-form-item label="出入库类型">
                <el-select
                  v-model="form.type"
                  clearable
                  class="edit-input"
                  placeholder="入库/出库"
                >
                  <el-option
                    v-for="(item, index) in options"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="调整数量">
                <el-input v-model="form.goodsCount" />
              </el-form-item>
            </el-col>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.cu === 2"
            type="primary"
            @click="submitAdjust"
          >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table"
                stripe border
                v-loading="crud.loading"
                :data="crud.data"
                size="small"
                style="width: 100%;"
                @selection-change="crud.selectionChangeHandler"
                @row-dblclick="crud.toDetailPlus('plasticDepositDetail', $event.id)"
                :cell-style="{'text-align':'center'}"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
<!--        <el-table-column v-if="columns.visible('id')" prop="id" label="id" />-->
        <el-table-column v-if="columns.visible('customerCode')" prop="customerCode" label="客户编码" />
        <el-table-column v-if="columns.visible('customerName')" prop="customerName" label="客户名称" />
        <el-table-column v-if="columns.visible('goodsCode')" prop="goodsCode" label="商品编码" />
        <el-table-column v-if="columns.visible('goodsName')" prop="goodsName" label="商品名称" />
        <el-table-column v-if="columns.visible('goodsCount')" prop="goodsCount" label="数量" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('updateTime')" prop="updateTime" label="修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
<!--        <el-table-column v-permission="['admin','plasticDeposit:edit','plasticDeposit:del','plasticDeposit:adjust']" label="操作" width="150px" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <udOperation-->
<!--              :data="scope.row"-->
<!--              :permission="permission"-->
<!--            />-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPlasticDeposit from '@/api/erp/plasticDeposit'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '塑箱押金',
  url: 'api/plasticDeposit',
  sort: 'id,desc',
  crudMethod: { ...crudPlasticDeposit },
  optShow: {
    add: false,
    edit: false,
    del: false,
    download: false,
    downloadDetail: false,
    leadingIn: false,
    enable: false,
    disable: false,
    pass: false,
    reject: false,
  }
})
const defaultForm = { id: null, customerId: null, goodsId: null, goodsCount: null, createTime: null, updateTime: null, companyId: null }
export default {
  name: 'PlasticDeposit',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      options: [
        {
          value: "2",
          label: "出库",
        },
        {
          value: "1",
          label: "入库",
        },
      ],
      chooseCustomer: [],
      permission: {
        add: ['admin', 'plasticDeposit:add'],
        edit: ['admin', 'plasticDeposit:edit'],
        del: ['admin', 'plasticDeposit:del'],
        import: ['admin', 'plasticDeposit:import'],
        export: ['admin', 'plasticDeposit:export'],
        copy: ['admin', 'plasticDeposit:copy'],
        upload: ['admin', 'plasticDeposit:upload'],
        download: ['admin', 'plasticDeposit:download'],
        adjust: ['admin', 'plasticDeposit:adjust'],
      },
      rules: {
        customerId: [
          { required: true, message: '客户id不能为空', trigger: 'blur' }
        ],
        goodsId: [
          { required: true, message: '商品id不能为空', trigger: 'blur' }
        ]
      }    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    adjust() {
      if(this.crud.selections[0]){
        const {
          id,
          customerId,
          customerCode,
          customerName,
          goodsId,
          goodsCode,
          goodsName,
        } = this.crud.selections[0];
        this.$set(this.form, "id", id);
        this.$set(this.form, "customerId", customerId);
        this.$set(this.form, "goodsId", goodsId);
      }
      this.crud.status.edit = 1;
    },
    submitAdjust() {
      crudPlasticDeposit.adjust(this.form).then((res) => {
        if (res.status === 200) {
          this.crud.status.edit = 0;
          this.$message.success(res.msg);
          this.crud.resetQuery();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  }
}
</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
